Replace deprecated functions, remove redundant local variables and closing tags

Should also fix the issue of extra new lines in the output of API calls.