decomped for ease and fixed some bugs