Some improvement and bug fixes. Need to refactor