improving code based on jshed's review