Compilation fixes and refactoring