Refactor to improve readability, added some business logic