Pull out the main computation into a function that can be called twice.

Also fixed an issue with the final alpha value.