Refactoring

Some optimizations