minor bugs fixed, refactored