cleaned up some code, hopefully got bit shifting stuff right