cleaned up expandkey function