Improved code structure - still needs some work