Refactored for readability and DOC added