Several improvements

*Refactoring to reduce a lot of code