First (somewhat) working port
first working version