DEV: not tested. Checked the code for hacks and other potentially overlooked problems. Made a couple of minor (unessential) changes