Refined the code a bit