Cleaned up the code a little bit and increased code re-use.