finished code to calculate layout of events