Split code out and fixed basic bugs. Now ready for box-creation code