Fixed variable expansion issues, general cleanup