cleanup and some changes to functions