rewritten from rubymotion