Added builder class for clearer code and easier unit testing. Further refactoring and added comments.