cleanup and code reordering