Clean up code + some refactoring