refactored primary functionality