Here's my pseudocode for all three parts of P24. This is not tested and may not be the best way, but I think the approach is solid.