Restore from original project

Restore from source I wrote a while back, refactored.