First actually working version (but could use testing)