This code block looks a bit ugly to me - will submit alternative implementation