rewrite the code to be more concise but still does not work