cleanup and split code