rewrite code in more idiomatic way
bug fix