Adding some improvements, suggested by abstractj