moderately cleaned up code