cleanup code and optimize data lookups