Code clean up and testing