Removed a bunch of ltrim and strtolower duplication; renamed variables to match.