Fixed and reformated version; needs testing