Refactoring the code a bit and adding some helper methods