code cleanup + cast test fix