reduce code, fix bugs