After swearing a lot, finally got this finished!