More refactoring

fixes one bug, and cleans up some code

@emord