Refactor and trim down logic