Did some code cleanup and fixed expansion