Pulled in code from dimerpaint
stole some more stuff from dimerpaint