ironed out some errors, refactoring...