make code nicer and bug fixes