Refactored main and test code