Cleaned up the code a bit in preparation for implementing the API methods.