Cleaned up code and made modular