Fix several errors
Refactoring