Refactored, single file version