It works now, but can surely be betterized.