lots of minor refactoring; space fixing; bug-checking