Some rework and code cleanup