p27 solution. TODO: cleanup code