WIP rewrite of Rafael work