Cleaned up code for registering documents