more bug fixes. Codes in a working state.