bufixes, minor change in flow and some code cleanup