Added in most of the agent code -- still needs to be refactored for proper use.