Refactoring and fixing compiling errors