Fixed build errors and refactoring