WIP5 - can now write values larger than 520 bytes, large code refactoring