fixing slightly broken code; refactoring still needed