Proper use of sprintf, and consolidate some code
Props for #185