Bug fixs and refactor; implement some member functions