code cleanup & fixed tests