code refactoring, fixed some bugs, few others still at large