fixed compiler errors, refactored code