Refactored main loop to facilitate testing, implemented basic tests