Did some refactoring to make it readable and easier to use