Cleaned up main controller with $window and scoping