fixed lots of internal bugs