code refactor and bug fixings