Refactoring

Organized code and added in a few fixes