This monolithic "action handler" is a bit hard to visually parse. Would be better to split it into separate handlers or at least separate functions that get called in each branch.